package com.muqi.app.user.widget;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

public class MyViewPager extends ViewPager{
    private int mCurrentPagePosition = 0;

    private GestureDetector mGestureDetector;
    View.OnTouchListener        mGestureListener;

    public MyViewPager(Context context) {
        super(context);
        mGestureDetector = new GestureDetector(context, new Detector());

    }

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, new Detector());

    }
    
//  @Override
//  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//
//      int height = 0;
//      for (int i = 0; i < getChildCount(); i++) {
//          View child = getChildAt(i);
//          child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//          int h = child.getMeasuredHeight();
//          if (h > height)
//              height = h;
//      }
//
//      heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
//
//      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//  }


    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        View child = getChildAt(mCurrentPagePosition);
        if(child!=null) {
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int height = child.getMeasuredHeight();
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    public void reMeasureCurrentPage(int position) {
        mCurrentPagePosition = position;
        requestLayout();
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        mGestureDetector.onTouchEvent(ev);
        return super.onTouchEvent(ev);
    }

    class Detector extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

            requestDisallowInterceptTouchEvent(true);

            return false;
        }
    }
}

